package buyers;


import cart.ShoppingCart;
import goods.Goods;
import goods.GoodsDataRead;
import order.Order;
import order.OrderDataRead;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class BuyersDAOArrayList  {
    private ArrayList<Buyer> buyerArrayList = new ArrayList<>();

    public boolean equal(String userName, String passWord) {
        for (Buyer e:buyerArrayList) {
            if(e.getUserName().equals(userName)&&e.getPassWord().equals(passWord)){
                return true;
            }
        }
        return false;
    }


    public void changePassWord(Object other) {
        Buyer B = (Buyer)other;
        for(int i = 0; i < buyerArrayList.size(); i++){
            if(buyerArrayList.get(i).getUserName().equals(B.getUserName())){
                buyerArrayList.remove(i);
                buyerArrayList.add(B);
                return;
            }
        }
    }


    public void addAccountData(Object other) {
        Buyer B = (Buyer)other;
        buyerArrayList.add(B);
    }

    public void addCartData(ArrayList<String> cart,String userName){
        for(int i = 0 ; i < buyerArrayList.size() ; i ++ ){
            if(buyerArrayList.get(i).getUserName().equals(userName)){
                for (String C:cart) {
                    String[] temp = C.split(" ");//编号 名称 单价 数量
                    ShoppingCart shoppingCart = new ShoppingCart(temp[1],temp[2],"1",temp[0]);
                    int flag = 0;
                    for(int j = 0 ; j < buyerArrayList.get(i).getShoppingCartArrayList().size() ; j ++ ){
                        if(buyerArrayList.get(i).getShoppingCartArrayList().get(j).equals(shoppingCart)){
                            buyerArrayList.get(i).getShoppingCartArrayList().get(j).setAmount(buyerArrayList.get(i).getShoppingCartArrayList().get(j).getAmount()+1);

                            flag = 1;
                            break;
                        }
                    }
                    if(flag == 0)
                        buyerArrayList.get(i).getShoppingCartArrayList().add(shoppingCart);
                }
                break;
            }
        }


    }


    public void addBuyersOrderData(String order,Buyer B) {
        for(int i = 0 ; i < buyerArrayList.size() ; i ++ ){
            if(buyerArrayList.get(i).equals(B)){
                buyerArrayList.get(i).getOrderArrayList().add(Long.valueOf(order));
            }
        }

    }

    public ArrayList<Buyer> getBuyerArrayList() {
        return buyerArrayList;
    }

    public void setBuyerArrayList(ArrayList<Buyer> buyerArrayList) {
        this.buyerArrayList = buyerArrayList;
    }

    public ArrayList<String> getUserCartArrayList(String userName){
        ArrayList<String> strings = new ArrayList<>();
        for (Buyer B:buyerArrayList) {
            if(B.getUserName().equals(userName)){
                for(int i = 0 ; i < B.getShoppingCartArrayList().size() ; i ++ ){
                    StringBuilder str = new StringBuilder();
                    str.append(B.getShoppingCartArrayList().get(i).getId()).append(" ").append(B.getShoppingCartArrayList().get(i).getName()).append(" ");
                    str.append(B.getShoppingCartArrayList().get(i).getPrice()).append(" ").append(B.getShoppingCartArrayList().get(i).getAmount());
                    String s = new String(str);
                    strings.add(s);
                }
                break;
            }
        }
        return strings;
    }

    public void changeCart(ArrayList<String> S,String userName){
        for(int i = 0 ; i < buyerArrayList.size() ; i ++ ){
            if(buyerArrayList.get(i).getUserName().equals(userName)){
                buyerArrayList.get(i).getShoppingCartArrayList().clear();
                if(S == null){
                    ShoppingCart SC = new ShoppingCart("null","0","0","0");
                    buyerArrayList.get(i).getShoppingCartArrayList().add(SC);
                    break;
                }
                for (String t:S) {
                    String[] temp = t.split(" ");
                    ShoppingCart SC = new ShoppingCart(temp[1],temp[2],temp[3],temp[0]);
                    buyerArrayList.get(i).getShoppingCartArrayList().add(SC);
                }

                break;
            }
        }
    }
    public void createOrder(OrderDataRead orderDataRead,Goods G, String userName){
        Date dNow = new Date( );
        SimpleDateFormat ft = new SimpleDateFormat ("yyyy.MM.dd&hh:mm:ss");
        SimpleDateFormat ft1 = new SimpleDateFormat ("yyyyMMddhhmmss");
        String time = ft.format(dNow);
        String orderName = ft1.format(dNow);
        Order O = new Order(orderName,G,time,userName);
        orderDataRead.getOrders().addOrderData(O);
    }
    public String checkOut(String userName, GoodsDataRead goodsDataRead,OrderDataRead orderDataRead){
        for(int i = 0 ; i < buyerArrayList.size() ; i ++){
            if(buyerArrayList.get(i).getUserName().equals(userName)){
                for (int j = 0 ; j < buyerArrayList.get(i).getShoppingCartArrayList().size() ; j ++){
                    for(int t = 0 ; t < goodsDataRead.getGoodsData().getGoodsArrayList().size() ; t ++ ){
                        if(goodsDataRead.getGoodsData().getGoodsArrayList().get(t).getId()==buyerArrayList.get(i).getShoppingCartArrayList().get(j).getId()) {
                            if(goodsDataRead.getGoodsData().getGoodsArrayList().get(t).getAmount()>=buyerArrayList.get(i).getShoppingCartArrayList().get(j).getAmount()) {
                                goodsDataRead.getGoodsData().getGoodsArrayList().get(t).setAmount(goodsDataRead.getGoodsData().
                                        getGoodsArrayList().get(t).getAmount() - buyerArrayList.get(i).getShoppingCartArrayList().get(j).getAmount());
                                Goods goods = new Goods(goodsDataRead.getGoodsData().getGoodsArrayList().get(t));
                                goods.setAmount(buyerArrayList.get(i).getShoppingCartArrayList().get(j).getAmount());
                                createOrder(orderDataRead,goods,userName);
                            }else{
                                return goodsDataRead.getGoodsData().getGoodsArrayList().get(t).getName()+"数量不足！";
                            }
                        }

                    }
                }
            }
        }
        return "结算成功";
    }
}
